﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditCountry
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If IsPostBack = False Then
                If Not Request.QueryString("cid") Is Nothing Then
                    fillData(Request.QueryString("cid"))
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("Select Id, Name, Overview, Thumbnail, Vieworder from ptn_Countries where Id=" & Id, 4)
        txtName.Text = Server.HtmlDecode(arr(1))
        txtOverview.Text = arr(2)
        If Not arr(3) Is Nothing Then
            ltrImg.Text = "<p><img src=""../images/img/" & arr(3) & """ align=""absmiddle"" /></p>"
        End If
        txtVieworder.Text = arr(4)
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Try
            Dim intId As Integer
            If txtVieworder.Text = "" Then
                txtVieworder.Text = 0
            End If
            If Not Request.QueryString("cid") Is Nothing Then
                Insert_Update_Delete_Record("UPDATE ptn_Countries Set [Name]='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "', Overview='" & Server.HtmlEncode(txtOverview.Text.Replace("'", "&#039;")) & "', Vieworder=" & Server.HtmlEncode(txtVieworder.Text.Replace("'", "&#039;")) & " where Id=" & Server.HtmlEncode(Request.QueryString("cid")))
                intId = Request.QueryString("cid")
            Else
                Insert_Update_Delete_Record("INSERT INTO ptn_Countries ([Name], Overview, Vieworder) values('" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtOverview.Text.Replace("'", "&#039;")) & "'," & Server.HtmlEncode(txtVieworder.Text.Replace("'", "&#039;")) & ")")
                Dim arr As Array
                arr = RetriveData("Select top 1 Id from ptn_Countries where Name='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "' order by Id desc", 0)
                intId = arr(0)
            End If
            UploadFile("ptn_Countries", "Thumbnail", fileUpload, intId, "/img")
            Response.Redirect("ViewCountry.aspx")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewCountry.aspx")
    End Sub

End Class